import {encrypt, decrypt} from "@/aes";
// store
import Vuex from 'vuex'
import Vue from "vue";
import storage from "@/store/storage";
Vue.use(Vuex)
// 用Vuex.Store对象用来记录token
const Store = new Vuex.Store({

    state: {
        // 存储token
        token: {
            tokenName: 'login-token',
            entityName: 'token-entity',
            value: null,
            entity: null,
            admin: {
                tokenHead: 'admin-token',
                tokenValue: null
            }
        }
    },

    getters: {
        getTokenValue(state){
            return state.token.value || storage.get(state.token.tokenName);
        },
        getTokenEntity(state){
            return state.token.entity || JSON.parse(storage.get(state.token.entityName));
        },
        getAdminTokenValue(state){
            return state.token.admin.tokenValue || storage.get(state.token.admin.tokenHead);
        }
    },
    mutations: {
        // 修改token，并将token存入localStorage
        setTokenValue(state,token) {
            state.token.value = token;
            storage.set(state.token.tokenName, token);
            console.log('store、localstorage保存token-value成功！');
        },
        setTokenEntity(state,entity){
            state.token.entity = entity;
            storage.set(state.token.entityName, entity);
            console.log('store、localstorage保存token-entity成功！');
        },
        setAdminTokenValue(state,token) {
            state.token.admin.tokenValue = token;
            storage.set(state.token.admin.tokenHead, token);
            console.log('store、localstorage保存admin-token-value成功！');
        },
        delTokenValue(state) {
            state.token.value = null;
            storage.remove(state.token.tokenName);
            state.token.entity = null;
            storage.remove(state.token.entityName);
            console.log('store、localstorage删除token成功！');
        },
        delAdminTokenValue(state) {
            state.token.admin.tokenValue = null;
            storage.remove(state.token.admin.tokenHead);
            console.log('store、localstorage删除admin-token成功！');
        },
    },

    actions: {
        // removeToken: (context) => {
        // context.commit('setToken')
        // }
    },
});

export default Store;
